package main;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

import mulan.classifier.MultiLabelLearner;
import weka.core.SelectedTag;
import classifiers.ConcurrentBinaryRelevance;
import classifiers.LibSVM;
import classifiers.helper.FeatureExtractionMethod;
import evaluation.BuildAndEvaluateMulanEvaluator;
import evaluation.MulanEvaluator;
import evaluation.PerformanceEvaluation;

public class SVMParameterSelection {
	
	static final String progress_file = "progress.txt";
	
	public static void main(String[] args) throws Exception {
		HashSet<String> completed_tasks = new HashSet<String>();
		HashSet<String> all_tasks = new HashSet<String>();
		File progress = new File(progress_file);
		if ( ! progress.exists() ) {
			progress.createNewFile();
		}
		Scanner jin = new Scanner(progress);
		while ( jin.hasNextLine() ) {
			String line = jin.nextLine();
			completed_tasks.add(line);
		}
		jin.close();
//		final String directory = "D:\\Data\\StackOverflow data\\arff\\";
		final String directory = "";
		int train_num = 6000;
		int test_num = 4000;
		ArrayList<String> train_files = new ArrayList<String>();
		ArrayList<String> test_files = new ArrayList<String>();
		for ( FeatureExtractionMethod method : FeatureExtractionMethod.values() ) {
			train_files.add(directory+"train_"+train_num+"_"+method+".arff");
			test_files.add(directory+"test_"+test_num+"_"+method+".arff");
		}
		final String xml_file = "mulan_clusters.xml";
		ArrayList<Double> costs = new ArrayList<Double>();
		ArrayList<Double> gammas = new ArrayList<Double>();
		for ( int pow = -3 ; pow <= 12 ; pow += 4 ) 
			costs.add(Math.pow(2, pow));
		for ( int pow = -10 ; pow <= 1 ; pow += 4 ) 
			gammas.add(Math.pow(2, pow));
		for ( Double cost : costs ) {
			for ( Double gamma : gammas ) {
				for ( int i = 0 ; i < train_files.size() ; ++i ) {
					String task_description = cost+";"+gamma+";"+train_files.get(i)+";"+test_files.get(i);
					all_tasks.add(task_description);
				}
			}
		}
		for ( String task : all_tasks ) {
			if ( ! completed_tasks.contains(task) ) {
				String s[] = task.split(";");
				final double cost = Double.parseDouble(s[0]);
				final double gamma = Double.parseDouble(s[1]);
				final String train_file = s[2];
				final String test_file = s[3];
				LibSVM svm = new LibSVM();
				svm.setKernelType(new SelectedTag(LibSVM.KERNELTYPE_RBF, LibSVM.TAGS_KERNELTYPE));
				svm.setCost(cost);
				svm.setGamma(gamma);
				svm.setProbabilityEstimates(true);
				String res_file = "res_"+train_file+"_svm_"+cost+"_"+gamma+".txt";
				MultiLabelLearner learner = new ConcurrentBinaryRelevance.ConcurrentBinaryRelevanceBuilder(svm).withThreshold(0.1).build();
				PerformanceEvaluation eval = new BuildAndEvaluateMulanEvaluator(train_file, test_file, xml_file, learner).evaluate();
				eval.flushToFile(res_file);
				PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(progress_file, true)));
				out.println(task);
				out.flush();
				out.close();
			}
		}
		
	}

}
